/**
 * knockout 自定义组件定义
 */
define(['knockout', 'platform.common'], function (ko) {
    /**
     * 树形组件，暂未用
     * @type {{init: ko.bindingHandlers.treeView.init}}
     */
    ko.bindingHandlers.treeView = {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var oViewModel = valueAccessor();
            var $ele = $(element).treeview(oViewModel.params).on('nodeSelected', function (event, data) {
                viewModel.nodeSelected(event, data);
            });

            oViewModel.treeview = function () {
                return $ele.treeview.apply($ele, arguments);
            }
        }
    };
    /**
     * jsTree组件，正在使用
     * @type {{init: ko.bindingHandlers.jsTreeView.init}}
     */
    ko.bindingHandlers.jsTreeView = {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var oViewModel = valueAccessor();
            var $ele = $(element).jstree(oViewModel.params);
            $(element).on('select_node.jstree', function (e, data) {
                viewModel.onTreeNodeSelected(e, data);
            });

            oViewModel.jstree = function () {
                return $ele.jstree.apply($ele, arguments);
            }
        }
    };
    /**
     * 列表组件
     * @type {{init: ko.bindingHandlers.tableView.init}}
     */
    ko.bindingHandlers.tableView = {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var oViewModel = valueAccessor();
            var $ele = $(element).bootstrapTable(oViewModel.params);
            oViewModel.bootstrapTable = function () {
                return $ele.bootstrapTable.apply($ele, arguments);
            }
        }
    };
    /**
     * 附件组件
     * @type {{init: ko.bindingHandlers.fileinput.init}}
     */
    ko.bindingHandlers.fileinput = {
        init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var oViewModel = valueAccessor();
            var $ele = $(element).fileinput(oViewModel.params).on('fileuploaded', function (event, data, previewId, index) {
                viewModel.onFileuploaded(event, data, previewId, index);
            });
            oViewModel.fileinput = function () {
                return $ele.fileinput.apply($ele, arguments);
            }
        }
    };
    /**
     * 日期组件
     * @type {{update: ko.bindingHandlers.datetimepicker.update}}
     */
    ko.bindingHandlers.datetimepicker = {
        update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
            var oViewModel = valueAccessor();
            var $ele = $(element).datetimepicker(oViewModel.params);
            oViewModel.dateTimePicker = function () {
                return $ele.datetimepicker.apply($ele, arguments);
            }
        }
    };
    /**
     * 模式对话框组件
     * @type {{init: ko.bindingHandlers.ikgModal.init, update: ko.bindingHandlers.ikgModal.update}}
     */
    ko.bindingHandlers.ikgModal = {
        init: function (element, valueAccessors) {
            var options = ko.utils.unwrapObservable(valueAccessors() || {});
            $(element).modal(options);
        },
        update: function (element, valueAccessors) {
            var options = ko.utils.unwrapObservable(valueAccessors() || {});

            $(element).modal(options.show() ? 'show' : 'hide');
        }
    };

});
// init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
//     // This will be called when the binding is first applied to an element
//     // Set up any initial state, event handlers, etc. here
// },
// update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
//     // This will be called once when the binding is first applied to an element,
//     // and again whenever any observables/computeds that are accessed change
//     // Update the DOM element based on the supplied values here.
// }